import 'babel-polyfill';
import AppDispatcher from '../dispatcher/dispatcher';
import Events from 'events';
import {_users,_doctors,_reports,_category,_hospitals} from './data';

const EventEmitter = Events.EventEmitter;

const CHANGE_EVENT = 'change';

let _films = {};                                //数据源
let _cate = {};                                 //分类状态
let _cateAllChecked = true;                     //分类默认全部选中
let _tags = {};                                 //标签状态
let _showList = [];                             //需要显示的数据
let _sortTime = 0;                              //排序方式
let _mainNav = {show:true,active:0};            //主导航状态
let _date = ()=>{
    let d = new Date();
    return (d.getFullYear()+"-"+d.getMonth()+"-"+d.getDate())
}
const initFilms = [
    {user:_users.u_1,report:_reports.r_1,operator:_doctors.d_1.name,date:_date(),part:"手部",hospital:_hospitals.h_1.name,url:'http://',category:_category.cat_1.name,share:false},
    {user:_users.u_1,report:_reports.r_1,operator:_doctors.d_1.name,date:_date(),part:"肩部",hospital:_hospitals.h_1.name,url:'http://',category:_category.cat_2.name,share:false},
    {user:_users.u_1,report:_reports.r_1,operator:_doctors.d_1.name,date:_date(),part:"足内侧",hospital:_hospitals.h_1.name,url:'http://',category:_category.cat_3.name,share:false},
    {user:_users.u_1,report:_reports.r_1,operator:_doctors.d_1.name,date:_date(),part:"脑",hospital:_hospitals.h_1.name,url:'http://',category:_category.cat_4.name,share:false},
    {user:_users.u_1,report:_reports.r_1,operator:_doctors.d_1.name,date:_date(),part:"胸部",hospital:_hospitals.h_1.name,url:'http://',category:_category.cat_5.name,share:false},
    {user:_users.u_1,report:_reports.r_1,operator:_doctors.d_1.name,date:_date(),part:"腿部",hospital:_hospitals.h_1.name,url:'http://',category:_category.cat_2.name,share:false},


];
const FilmStore = Object.assign({},EventEmitter.prototype,{
    initCate(initData,outData){
        for(let key in initData){
            outData[key] = initData[key];
            outData[key].checked = true;
        }
        return outData
    },
    allChecked(){
        return _cateAllChecked
    },
    ctrlNav(obj){
        if(obj !== undefined){
            _mainNav.show = obj.show;
            _mainNav.active = obj.active;
        }
        return _mainNav
    },
    create(obj){
        let id = (+new Date() + Math.floor(Math.random() * 999999)).toString(36);
        _films[id] = {
            id: id,
            user: obj.user,
            report: obj.report,
            operator: obj.operator,
            date: obj.date,
            part: obj.part,
            hospital: obj.hospital,
            url: obj.url,
            category: obj.category,
            share: false
        };
    },
    update(id,updates){
        _films[id] = Object.assign({},_films[id],updates)
    },
    getAll(){
        return _films;
    },
    getFilm(id){
        return _films[id]
    },
    getSome(ids){
        let some = {};
        for(let id in _films){
            if(ids.indexOf(id) !== -1){
                some[id] = _films[id]
            }
        }
        return some
    },
    getList(){
        let listIds = [];
        if(_cateAllChecked){
           return this.getAll()
        }else{
            let _cats = [];
            for(let _c in _cate){
                if(_cate[_c].checked){
                    _cats.push(_cate[_c].name)
                }
            }
            for(let film in _films){
                if( _cats.indexOf(_films[film].category) !== -1){
                    listIds.push(film)
                }
            }
            return this.getSome(listIds)
        }
    },
    getCats(){
        return _cate
    },
    updateFilter(filter,data){
        switch(filter){
            case 'CATE':
                _cate = data
                for(let _c in _cate){
                    if(!_cate[_c].checked){
                        _cateAllChecked = false;
                        break
                    }else{
                        _cateAllChecked = true;
                    }
                }
                break;
            case 'TAG':

                break;
            default:
                return
        }
        this.getList()
    },
    emitChange(){
        this.emit(CHANGE_EVENT)
    },
    addChangeListener(callback){
        this.on(CHANGE_EVENT,callback)
    },
    removeChangeListener(callback){
        this.removeListener(CHANGE_EVENT,callback)
    }
});
AppDispatcher.register((action) => {
    let obj;
    let id = '';
    let filter = '';
    let show;
    let active;
    let data;
    switch (action.actionType){
        case "FILM_CREATE":
            obj = action.data;
            FilmStore.create(obj);
            FilmStore.emitChange();
            break;
        case 'FILM_UPDATE':
            obj = action.data;
            id = action.id;
            FilmStore.update(id,obj);
            FilmStore.emitChange();
            break;
        case 'GET_LIST':
            FilmStore.getList();
            FilmStore.emitChange();
            break;
        case 'GET_FILM':
            id = action.id;
            FilmStore.getFilm(id);
            FilmStore.emitChange();
            break;
        case 'GET_CATS':
            FilmStore.getCats('store');
            FilmStore.emitChange();
            break;
        case 'FILTER_UPDATE':
            obj = action.data;
            filter = action.filter;
            FilmStore.updateFilter(filter,obj);
            FilmStore.emitChange();
            break;
        case 'CTRL_NAV':
            obj = action.data;
            FilmStore.ctrlNav(obj);
            FilmStore.emitChange();
            break;
        default:
            return null;
    }
});

initFilms.forEach((film) => {
    FilmStore.create(film);
});

FilmStore.initCate(_category,_cate);
export default FilmStore;